package cn.wang.web.sysadmin;

import cn.wang.entity.sysadmin.Dept;
import cn.wang.entity.sysadmin.Role;
import cn.wang.entity.sysadmin.User;
import cn.wang.service.sysadmin.DeptService;
import cn.wang.service.sysadmin.RoleService;
import cn.wang.service.sysadmin.UserService;
import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Set;

/**
 * Created by john on 2017/11/1.
 */
@Controller
public class UserAction extends BaseAction implements ModelDriven<User> {
    //分页信息
    private Pagination page = new Pagination();
    //用户列表
    private List<User> userList;
    //部门列表
    private List<Dept> deptList;
    //用户
    private User model = new User();
    //角色列表
    private List<Role> roleList;

    private String[] roleIds;
    //角色集(用户包括那些角色 拼装r1,r2,r3)
    private String userRoleStr;
    //激活码
    private String code;

    private String id;
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;


    //分页
    public String list() {
        //根据用户等级不同,显示不同数据
        User user = getUser();
        page = userService.findPage(page.getPageNo(), page.getPageSize(), user);
        page.setUrl("userAction_list");
        push(page);
        return "list";
    }

    //添加用户前 先查询所有部门列表 所属领导
    public String tocreate() {

        userList = userService.findAll();
        deptList = deptService.findAll();
        return "tocreate";
    }

    //查寻单个部门信息
    public String toview() {
        model = userService.findById(model.getId());
        push(model);
        return "toview";
    }

    //添加部门
    public String insert() {
        //设置更新日期 更新部门
        model.setCreateBy(getUser().getId());
        model.setCreateDept(model.getDept().getId());
        //设置状态
        model.setState(0);
        userService.save(model);
        return "rlist";

    }

    //更新部门 先查询
    public String toupdate() {
        if (model.getId() == null) {
            return "toupdate";
        }
        //查询所有部门
        deptList = deptService.findAll();
        model = userService.findById(model.getId());
        push(model);
        //根据id查询老部门
        //移除本部门
        //userList.remove(model);
        return "toupdate";
    }

    public String update() {
        userService.update(model);

        return "rlist";
    }

    //删除部门 多个部门删除时 id 字符类型id会 拼接在一起(,空格)
    public String delete() {
        if (model.getId() == null) {
            return "rlist";
        }
        String[] ids = model.getId().split(", ");
        userService.delete(ids);
        return "rlist";
    }

    //角色列表
    public String torole() {
        model = userService.findById(model.getId());
        push(model);
        Set<Role> roleSet = model.getRoleSet();
        StringBuffer sb = new StringBuffer();
        for (Role role : roleSet) {
            sb.append(role.getName()).append(",");

        }
        userRoleStr = sb.toString();
        roleList = roleService.findAll();
        return "torole";
    }

    //分配列表
    public String role() {
        userService.updateRole(model.getId(), roleIds);
        //TODO 不能保存 跳转页面
        return "rlist";
    }

    //激活邮箱
    public String activty() {
        model = userService.activty(model);
        if (model == null || model.getState() == 0) {
            ActionContext.getContext().getValueStack().set("errorInfo", "激活失败");
        }

        return "login";
    }

    public void setPage(Pagination page) {
        this.page = page;
    }


    @Override
    public User getModel() {
        //将字段名设为model使一行代码两用 1模型驱动 2 字段set方法
        return model;
    }

    public List<User> getUserList() {
        return userList;
    }

    public List<Dept> getDeptList() {
        return deptList;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public String getUserRoleStr() {
        return userRoleStr;
    }

    public void setRoleIds(String[] roleIds) {
        this.roleIds = roleIds;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setId(String id) {
        this.id = id;
    }
}
